package com.hbx.platform.auth.controller;

import com.hbx.platform.auth.Dto.RoleDto;
import com.hbx.platform.auth.domain.Role;
import com.hbx.platform.auth.query.RoleQuery;
import com.hbx.platform.auth.service.IRoleService;
import com.hbx.platform.base.annotation.AutoPermission;
import com.hbx.platform.base.utils.AjaxResult;
import com.hbx.platform.base.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
@AutoPermission(name = "角色管理")
public class RoleController {

    @Autowired
    public IRoleService roleService;

    /**
     * 保存和修改公用的
     *
     * @param role 传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @AutoPermission(name = "添加或修改角色", desc = "id为null添加，反之修改")
    public AjaxResult saveOrUpdate(@RequestBody Role role) {
        try {
            if (role.getId() != null) {
                roleService.update(role);
                return AjaxResult.me().setMessage("修改成功");
            } else {
                roleService.save(role);
                return AjaxResult.me().setMessage("添加成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("报错：" + e.getMessage());
        }
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    @AutoPermission(name = "删除角色",desc = "根据id删除角色")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            roleService.delete(id);
            return AjaxResult.me().setMessage("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！" + e.getMessage());
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public AjaxResult queryById(@PathVariable("id") Long id) {
        try {
            Role role = roleService.queryById(id);
            return AjaxResult.me().setData(role);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取一个失败！" + e.getMessage());
        }
    }


    /**
     * 查看所有的员工信息
     *
     * @return
     */
    @GetMapping
    public AjaxResult getAll() {

        try {
            List<Role> list = roleService.getAll();
            return AjaxResult.me().setData(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取所有失败！" + e.getMessage());
        }
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping
    public AjaxResult page(@RequestBody RoleQuery query) {
        try {
            PageList<Role> pageList = roleService.page(query);
            return AjaxResult.me().setData(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取分页数据失败！" + e.getMessage());
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    @AutoPermission(name = "批量删除角色",desc = "批量删除")
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        try {
            Integer count = roleService.batchDelete(ids);
            return AjaxResult.me().setMessage("删除了"+count+"条记录");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("批量删除失败");
        }
    }

    @PutMapping("/saveOrUpdateTree")
    @AutoPermission(name = "添加或修改权限树", desc = "id为null添加，反之修改")
    public AjaxResult saveOrUpdateTree(@RequestBody RoleDto roleDto) {
        try {
            if (roleDto.getRole().getId() != null) {
                roleService.update(roleDto.getRole());
                roleService.updateTree(roleDto);
                return AjaxResult.me().setMessage("修改成功");
            } else {
                roleService.save(roleDto.getRole());
                roleService.saveTree(roleDto);
                return AjaxResult.me().setMessage("添加成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("报错：" + e.getMessage());
        }
    }

    @GetMapping("/permission/{id}")
    public AjaxResult getPermissionsId(@PathVariable("id") Long id){
        try {
            List<Long> ids = roleService.getPermissionsId(id);
            return AjaxResult.me().setData(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("报错：" + e.getMessage());
        }
    }



}
